import "./index.scss"
import { Card, Form, Input, Button, message } from "antd";
import { useDispatch } from "react-redux";
import { fetchLogin } from "@/store/modules/user";
import { useNavigate } from "react-router-dom";


const Login = () => {
    // redux 触发方法
    const dispatch = useDispatch()
    // 路由跳转函数
    const navigate = useNavigate()
    const onFinish = async (values) => {
        // console.log(values);
        // 登录方法写在redux里面
        await dispatch(fetchLogin(values))
        // 上面加 async await，作用是等待接口调用完毕在执行下面的方法
        // 提示信息
        message.success('登录成功')
        // 跳转到首页
        navigate('/')
    }
    return (
        <div className="login">
            <Card className="login-con">
                <div className="login-title">请登录</div>
                {/* initialValues 给输入框一个默认值 */}
                <Form onFinish={onFinish} initialValues={{ mobile: '13800000002', code: '246810' }}>
                    <Form.Item
                        name="mobile"
                        rules={[
                            {
                                required: true,
                                message: '请输入手机号'
                            },
                            // 正则匹配手机号
                            {
                                pattern: /^1[3-9]\d{9}$/,
                                message: '请输入正确的手机号'
                            }
                        ]}
                    >
                        <Input size="large" placeholder="请输入手机号" />
                    </Form.Item>
                    <Form.Item
                        name="code"
                        rules={[
                            {
                                required: true,
                                message: '请输入验证码'
                            }
                        ]}
                    >
                        <Input size="large" placeholder="请输入验证码" />
                    </Form.Item>
                    <Form.Item>
                        <Button type="primary" htmlType="submit" size="large" block>登录</Button>
                    </Form.Item>
                </Form>
            </Card>
        </div>
    )
}

export default Login